package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.Batches;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class PeriodfoAdapter extends RecyclerView.Adapter<PeriodfoAdapter.StaffViewHolder> {
    private Context mContext;
    private List<List<Batches.DataBean>> list = new ArrayList<>();
    protected OnItemClickListener mItemClickListener;

    public PeriodfoAdapter(Context mContext) {
        super();
        this.mContext = mContext;
    }

    public void setList(List<List<Batches.DataBean>> list) {
        this.list = list;
    }

    @Override
    public StaffViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.peroid_item, parent, false);
        final StaffViewHolder viewHolder = new StaffViewHolder(rootView);
        //itemView 的点击事件
        if (mItemClickListener != null) {
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(viewHolder.getAdapterPosition(), v);
                }
            });
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(StaffViewHolder holder, int position) {
        List<Batches.DataBean> dataBeen = list.get(position);
        Batches.DataBean shopInfo = dataBeen.get(0);
        if (shopInfo.isChecked()) {
            holder.ll_root.setBackgroundResource(R.drawable.btn_oval_bg);
            holder.tv_name.setTextColor(mContext.getResources().getColor(R.color.tab_color_true));
            holder.tv_number.setTextColor(mContext.getResources().getColor(R.color.tab_color_true));
        } else {
            holder.ll_root.setBackgroundResource(R.drawable.btn_oval__false_bg);
            holder.tv_name.setTextColor(mContext.getResources().getColor(R.color.tab_color_false));
            holder.tv_number.setTextColor(mContext.getResources().getColor(R.color.tab_color_false));
        }
        holder.tv_name.setText("保质期 至 " + Tools.SubTime2(shopInfo.getDue_date() + ""));
        holder.tv_number.setText("库存：" + shopInfo.getStock());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class StaffViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_number;
        LinearLayout ll_root;

        public StaffViewHolder(View view) {
            super(view);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_number = (TextView) view.findViewById(R.id.tv_number);
            ll_root = (LinearLayout) view.findViewById(R.id.ll_root);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

}
